<?php
	class sotietkiemdb
	{
		public function Them($MaSoTietKiem = "",
							 $MaKH = "", 
							 $MaLoaiKyHan = 0,
						     $MaHTLanhLai = 0,
							 $LaiSuatGui = 0,
							 $SoTienGui = 0,
							 $NgayMoSo = "",
							 $NgayDenHan = "",
							 $NgayLanhLai = "",
							 $LaiDaTra = 0,
							 $NgayNhapGoc = "",
				 			 $LaiNhapGoc = 0,
							 $NgayTatToan = "",
							 $Khoa = 0,
							 $SoDuKyHanTT = 0,
							 $SoTienNhan = 0)
		{
			$sql = "INSERT INTO sotietkiem(MaSoTietKiem, MaKH, MaLoaiKyHan, MaHTLanhLai, LaiSuatGui, SoTienGui, NgayMoSo, NgayDenHan, NgayLanhLai, LaiDaTra, NgayNhapGoc, LaiNhapGoc, NgayTatToan, Khoa, SoDuKyHanTT, SoTienNhan)
					VALUES('$MaSoTietKiem',
							 '$MaKH', 
							 $MaLoaiKyHan,
						     $MaHTLanhLai,
							 $LaiSuatGui,
							 $SoTienGui,
							 '$NgayMoSo',
							 '$NgayDenHan',
							 '$NgayLanhLai',
							 $LaiDaTra,
							 '$NgayNhapGoc',
				 			 $LaiNhapGoc,
				 			 '$NgayTatToan',
				 			 $Khoa,
							 $SoDuKyHanTT,
							 $SoTienNhan)";		
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		public function ThemSoTietKiem($MaSoTietKiem = "",
				$MaKH = "",
				$MaLoaiKyHan = 0,
				$MaHTLanhLai = 0,
				$LaiSuatGui = 0,
				$SoTienGui = 0,
				$NgayMoSo = "",
				$NgayDenHan = "",
				$NgayLanhLai ="",
				$SoDuKyHanTT = 0,
				$SoTienNhan = 0)
		{
			$sql = "INSERT INTO sotietkiem(MaSoTietKiem, MaKH, MaLoaiKyHan, MaHTLanhLai, LaiSuatGui, SoTienGui, NgayMoSo, NgayDenHan, NgayLanhLai, Khoa, LaiDaTra, NgayNhapGoc, LaiNhapGoc, NgayTatToan, SoDuKyHanTT, SoTienNhan)
			VALUES('$MaSoTietKiem',
			'$MaKH',
			$MaLoaiKyHan,
			$MaHTLanhLai,
			$LaiSuatGui,
			$SoTienGui,
			'$NgayMoSo',
			'$NgayDenHan',
			'$NgayLanhLai', 0, 0, '$NgayDenHan', 0, '$NgayDenHan', $SoDuKyHanTT, $SoTienNhan)";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		public function CapNhat($MaSoTietKiem = "",
							 	$MaKH = "", 
							 	$MaLoaiKyHan = 0,
						     	$MaHTLanhLai = 0,
							 	$LaiSuatGui = 0,
							 	$SoTienGui = 0,
							 	$NgayMoSo = "",
							 	$NgayDenHan = "",
							 	$NgayLanhLai = "",
							 	$LaiDaTra = 0,
							 	$NgayNhapGoc = "",
				 			 	$LaiNhapGoc = 0,
							 	$NgayTatToan = "",
							 	$Khoa = 0,
							 	$SoDuKyHanTT = 0,
								$SoTienNhan = 0)
		{
			$sql = "UPDATE sotietkiem SET 
			SoTienGui = $SoTienGui,
			NgayMoSo = '$NgayMoSo',
			NgayDenHan = '$NgayDenHan',
			NgayLanhLai = '$NgayLanhLai',
			LaiDaTra = $LaiDaTra,
			NgayNhapGoc = '$NgayNhapGoc',
			LaiNhapGoc = $LaiNhapGoc,
			NgayTatToan = '$NgayTatToan',
			Khoa = $Khoa,
			SoDuKyHanTT = $SoDuKyHanTT,
			SoTienNhan = $SoTienNhan";
			
			if($MaKH != "")
				$sql .= ", MaKH = '$MaKH'";
			
			if($MaLoaiKyHan > 0)
				$sql .= ", MaLoaiKyHan = $MaLoaiKyHan";
			
			if($MaHTLanhLai > 0)
				$sql .= ", MaHTLanhLai = $MaHTLanhLai";
			
			if($LaiSuatGui > 0)
				$sql .= ", LaiSuatGui = $LaiSuatGui";
			
			$sql .= " WHERE MaSoTietKiem = '$MaSoTietKiem'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhatKhoa($MaSoTietKiem = "",
									$Khoa = 0)
		{
			$sql = "update sotietkiem set Khoa = ".dataservice::QuoteSmart($Khoa)."
					where MaSoTietKiem = ".dataservice::QuoteSmart($MaSoTietKiem);
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function LayDanhSach($MaSoTietKiem = "",
							 		$MaKH = "", 
							 		$MaLoaiKyHan = 0,
						     		$MaHTLanhLai = 0,
							 		$LaiSuatGui = 0,
							 		$SoTienGui = 0,
							 		$NgayMoSo = "",
							 		$NgayDenHan = "",
							 		$NgayLanhLai = "",
							 		$LaiDaTra = 0,
							 		$NgayNhapGoc = "",
				 			 		$LaiNhapGoc = 0,
							 		$NgayTatToan = "",
							 		$Khoa = 0,
							 		$SoDuKyHanTT = 0,
									$SoTienNhan = 0)
		{
			$sql = "SELECT * 
					FROM sotietkiem
					WHERE (1)";
			
			if($MaSoTietKiem != "")
				$sql .= " AND MaSoTietKiem = '$MaSoTietKiem'";
			
			if($MaKH != "")
				$sql .= " AND MaKH = '$MaKH'";
			
			if($MaLoaiKyHan > 0)
				$sql .= " AND MaLoaiKyHan = $MaLoaiKyHan";
			
			if($MaHTLanhLai > 0)
				$sql .= " AND MaHTLanhLai = $MaHTLanhLai";
			
			if($LaiSuatGui > 0)
				$sql .= " AND LaiSuatGui = $LaiSuatGui";
			
			if($SoTienGui > 0)
				$sql .= " AND SoTienGui = $SoTienGui";
			
			if($NgayMoSo != "")
				$sql .= " AND NgayMoSo = '$NgayMoSo'";
			
			if($NgayDenHan != "")
				$sql .= " AND NgayDenHan = '$NgayDenHan'";
			
			if($NgayLanhLai != "")
				$sql .= " AND NgayLanhLai = '$NgayLanhLai'";
			
			if($LaiDaTra > 0)
				$sql .= " AND LaiDaTra = $LaiDaTra";
			
			if($NgayNhapGoc != "")
				$sql .= " AND NgayNhapGoc = '$NgayNhapGoc'";
				
			if($LaiNhapGoc > 0)
				$sql .= " AND LaiNhapGoc = $LaiNhapGoc";
			
			if($NgayTatToan != "")
				$sql .= " AND NgayTatToan = '$NgayTatToan'";
				
			if($Khoa > 0)
				$sql .= " AND Khoa = $Khoa";
			
			if($SoDuKyHanTT > 0)
				$sql .= " AND SoDuKyHanTT = $SoDuKyHanTT";
			
			if($SoTienNhan > 0)
				$sql .= " AND SoTienNhan = $SoTienNhan";

			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function LayDanhSachKhongKyHan()
		{
			$sql = "SELECT * FROM sotietkiem, loaikyhan WHERE sotietkiem.MaLoaiKyHan = loaikyhan.MaLoaiKyHan AND loaikyhan.KyHan =0";
				
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function LayDanhSachCoKyHan()
		{
			$sql = "SELECT * FROM sotietkiem, loaikyhan WHERE sotietkiem.MaLoaiKyHan = loaikyhan.MaLoaiKyHan AND loaikyhan.KyHan <>0";
		
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
	}
?>